package com.zzptc.zzsgz.bdsjws.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import com.github.lzyzsd.randomcolor.RandomColor;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.views.CircleTextImageView;
import com.zzptc.zzsgz.bdsjws.vo.Contact;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;




/**
 * Created by zzsgz on 2017/6/6.
 */

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyHoldView> {

    private List<Contact> list;
    private List<Contact> soslist= MyApplication.getSoslist();
    static int count;
    private TextView tv_num;


    public  int getCount(int count){
        return this.count=count;
    }
    public ContactAdapter(List<Contact> list,TextView tv_num)
    {
        this.list=list;
        this.tv_num=tv_num;
    }



    @Override
    public MyHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.show_contact_item,parent,false);
        MyHoldView myHoldView=new MyHoldView(view);
        return myHoldView;
    }

    @Override
    public void onBindViewHolder(MyHoldView holder, int position) {
          //在绑定之前要有数据
        Contact contact=list.get(position);
        holder.ctiv.setText(contact.getName().substring(1));
        //加颜色
        RandomColor randomColor= new RandomColor();
       int color= randomColor.randomColor();
        holder.ctiv.setFillColor(color);
        holder.tv_username.setText(contact.getName());
        holder.tv_tel.setText(contact.getPhonenum());
        holder.tv_addr.setText(contact.getAddress());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }



    class MyHoldView extends RecyclerView.ViewHolder implements View.OnClickListener{
        @BindView(R.id.ctiv)
        CircleTextImageView ctiv;
        @BindView(R.id.tv_name)
        TextView tv_username;
        @BindView(R.id.tv_tel)
        TextView tv_tel;
        @BindView(R.id.tv_addr)
        TextView tv_addr;
        @BindView(R.id.cb_choose)
        CheckBox cb_choose;


        public MyHoldView(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(this);//给组件设置监听
        }

        @Override
        public void onClick(View view) {

            int position=getAdapterPosition();
            Contact c=list.get(position);
            String tel=c.getPhonenum().replace(" ","").replace("-","");//电话号码
           // Toast.makeText(MyApplication.getContext(), c.getName()+"===========", Toast.LENGTH_SHORT).show();
            //1、加上RecyvlerView点击事件  2、只能选三个人 多选框要选中（第一小步：点击后多选框选中  第二小步：可以取消
            // 第三小步：限制数量）  3、只能选手机号码
            if(tel.length()==11&&tel.matches("^1[3578]\\d{9}$")) {
                if (cb_choose.isChecked()) {
                    cb_choose.setChecked(false);
                    soslist.remove(c);
                    tv_num.setText("("+MyApplication.getSoslist().size()+")");
                } else {

                    if (soslist.size()< 3) {
                        cb_choose.setChecked(true);
                        if (!soslist.contains(c)) {
                            soslist.add(c);//添加之前判断是否已经添加
                        }
                    } else {
                        Toast.makeText(MyApplication.getContext(), "不能超过三个人", Toast.LENGTH_SHORT).show();
                    }
                    tv_num.setText("("+MyApplication.getSoslist().size()+")");
                }


            }else{
                Toast.makeText(MyApplication.getContext(), "请选择手机号码", Toast.LENGTH_SHORT).show();
            }

        }
    }
}
